package com.itheima.chapter10.thread.demo11;

public class MyRunnable implements Runnable {
    private  int ticket =100;
    @Override
    public void run() {
       System.out.println("this:"+this);
       //同步方法的锁对象是  实现类对象 new MyRunnable() 即this

        while(true){
            payTicket();
        }
    }

    public synchronized void payTicket() {
        if (ticket > 0) {
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "--->正在读第" + ticket + "页");
            ticket--;

        }
    }
}
